//
//  PMinePrivacySettingViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/23.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMinePrivacySettingViewController.h"
#import "CompanyPrivacySettingFilterViewController.h"
#import "CompanyPrivacySettingModel.h"

#import "LYPrivacySettingController.h"

@interface PMinePrivacySettingViewController ()<CompanyPrivacySettingFilterViewControllerDelegate>
{
    PSCell *lookCell;
    PSCell *pushCell;
    PSCell *shownameCell;
    PSCell *blacklistCell;
}
@property (nonatomic, strong) CompanyPrivacySettingModel *infoModel;

@end

@implementation PMinePrivacySettingViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"隐私设置";
    [self loadData];
}
- (void)loadData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    
    [BaseNetManager requestWithMethod:POST path:Person_GetPrivacyInfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if (data && [data isKindOfClass:[NSDictionary class]]) {
                self.infoModel = [[CompanyPrivacySettingModel alloc] init];
                [self.infoModel setValuesForKeysWithDictionary:data];
            }
        }
        [self initUI];
        SVPHUDDismiss;
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)initUI {
    __weak __typeof(self)weakSelf = self;
    lookCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, 15*Scale, WIDTH-60*Scale, PSCellHeight) title:@"谁可以查看我的简历" text:((self.infoModel && self.infoModel.viewShow.length)?self.infoModel.viewShow:nil) placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    lookCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf jumpToLookVC];
    };
    [self.view addSubview:lookCell];
    
    pushCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(lookCell), WIDTH-60*Scale, PSCellHeight) title:@"简历推送给哪些人" text:((self.infoModel && self.infoModel.pushShow.length)?self.infoModel.pushShow:nil) placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    pushCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf jumpToPushVC];
    };
    [self.view addSubview:pushCell];

    NSString *name = @"";
//    if ([self.infoModel.display_name isEqualToString:@"3"]) {
//        name = @"先生/女士";
//    } else if ([self.infoModel.display_name isEqualToString:@"4"]) {
//        name = @"我的昵称";
//    }
    name = self.infoModel.display_name_cn;
    shownameCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(pushCell), WIDTH-60*Scale, PSCellHeight) title:@"访问主页时显示的花名" text:name placeholder:((self.infoModel && self.infoModel.videoShow.length)?self.infoModel.videoShow:@"请选择") cellType:PSCellTypeOfIndicator];
    shownameCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf jumpToShownameVC];
    };
    [self.view addSubview:shownameCell];
    
    blacklistCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(shownameCell), WIDTH-60*Scale, PSCellHeight) title:@"屏蔽企业" text:nil placeholder:@"请编辑" cellType:PSCellTypeOfIndicator];
    blacklistCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf jumpToShieldVC];
    };
    
    if (self.infoModel.shield_company.count> 0) {
        NSString *nameString = @"";
        for (NSString *name in self.infoModel.shield_company) {
            nameString = [NSString stringWithFormat:@"%@ %@",nameString,name];
        }
        nameString = [nameString substringFromIndex:1];
        blacklistCell.textField.text = nameString;
    }else{
        blacklistCell.textField.text = @"";
    }
    [self.view addSubview:blacklistCell];
}
- (void)okBtnTap {
    
}

- (void)companyPrivacySettingFilterViewControllerType:(CompanyPrivacySettingFilterType)type info:(NSString *)info selectType:(NSString *)typestring selectArray:(NSArray *)array{
    switch (type) {
        case CompanyPrivacySettingFilterTypeOfLook: {
            lookCell.textField.text = info;
            self.infoModel.view = typestring;
            self.infoModel.view_list = [NSMutableArray arrayWithArray:array];
        } break;
        case CompanyPrivacySettingFilterTypeOfPush: {
            pushCell.textField.text = info;
            self.infoModel.push = typestring;
            self.infoModel.push_list = [NSMutableArray arrayWithArray:array];
        } break;
        case CompanyPrivacySettingFilterTypeOfName: {
            shownameCell.textField.text = info;
            self.infoModel.display_name = typestring;
        } break;
        case PersonalPrivacySettingFilterTypeOfName: {
            shownameCell.textField.text = info;
            self.infoModel.display_name = typestring;
        } break;
        case PersonalPrivacySettingFilterTypeOfLook: {
            lookCell.textField.text = info;
            self.infoModel.view = typestring;
            self.infoModel.view_list = [NSMutableArray arrayWithArray:array];
        } break;
        case PersonalPrivacySettingFilterTypeOfPush: {
            pushCell.textField.text = info;
            self.infoModel.push = typestring;
            self.infoModel.push_list = [NSMutableArray arrayWithArray:array];
        } break;
        default:
            break;
    }
}
#pragma mark -- Action
- (void)jumpToLookVC{
    CompanyPrivacySettingFilterViewController *CPSFVC = [[CompanyPrivacySettingFilterViewController alloc] init];
    if (self.infoModel && self.infoModel.view_list.count) {
        CPSFVC.itemDataArray = self.infoModel.view_list;
    }
    CPSFVC.selectType = self.infoModel.view;
    CPSFVC.delegate = self;
    CPSFVC.isPerson = YES;
    CPSFVC.type = PersonalPrivacySettingFilterTypeOfLook;
    [self.navigationController pushViewController:CPSFVC animated:YES];
}

- (void)jumpToPushVC{
    CompanyPrivacySettingFilterViewController *CPSFVC = [[CompanyPrivacySettingFilterViewController alloc] init];
    if (self.infoModel && self.infoModel.push_list.count) {
        CPSFVC.itemDataArray = self.infoModel.push_list;
        CPSFVC.pushBanDataArray = self.infoModel.push_bank_list;
    }
    CPSFVC.selectType = self.infoModel.push;
    CPSFVC.delegate = self;
    CPSFVC.isPerson = YES;
    CPSFVC.type = PersonalPrivacySettingFilterTypeOfPush;
    [self.navigationController pushViewController:CPSFVC animated:YES];}

- (void)jumpToShownameVC{
    CompanyPrivacySettingFilterViewController *CPSFVC = [[CompanyPrivacySettingFilterViewController alloc] init];
    if (self.infoModel && self.infoModel.display_name.length) {
        CPSFVC.display_name = self.infoModel.display_name;
    }
    CPSFVC.selectType = self.infoModel.display_name;
    CPSFVC.delegate = self;
    CPSFVC.isPerson = YES;
    CPSFVC.type = PersonalPrivacySettingFilterTypeOfName;
    [self.navigationController pushViewController:CPSFVC animated:YES];
}

- (void)jumpToShieldVC{
    LYPrivacySettingController *shieldVC = [[LYPrivacySettingController alloc] init];
    __weak typeof(self) weakSelf = self;
    shieldVC.shieldMax = [self.infoModel.shield_max integerValue];
    shieldVC.shieldArray = self.infoModel.shield_company;
    shieldVC.block = ^(NSArray *array){
        [weakSelf showShieldCompany:array];
    };
    [self.navigationController pushViewController:shieldVC animated:YES];
}

- (void)showShieldCompany:(NSArray *)array{
    self.infoModel.shield_company = [NSMutableArray arrayWithArray:array];
    if (array.count> 0) {
        NSString *nameString =@"";
        for (NSString *name in array) {
            nameString = [NSString stringWithFormat:@"%@ %@",nameString,name];
        }
        nameString = [nameString substringFromIndex:1];
        blacklistCell.textField.text = nameString;
    }else{
        blacklistCell.textField.text = @"";
    }
}

@end
